/**
 * 
 */
package com.colombounplug.geoshopper;

import java.util.ArrayList;

import android.app.ListActivity;
import android.database.Cursor;
import android.database.CursorJoiner.Result;
import android.os.Bundle;
import android.util.Log;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.EditText;
import android.widget.SimpleCursorAdapter;

/**
 * @author anuradha.uduwage
 *
 */
public class ShoppingListTab extends ListActivity {
	
	private ShoppingListDbAdapter mDbHelper;
	public static final int INSERT_ID = Menu.FIRST;
	public static final int DELETE_ID= Menu.FIRST + 1;
	private EditText myEditText;
    
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.shoplist);
        myEditText = (EditText) findViewById(R.id.myEditText);
        myEditText.setVisibility(View.VISIBLE);
        mDbHelper = new ShoppingListDbAdapter(this);
        mDbHelper.open();
        populateData();

        //register each item in the context menu
        registerForContextMenu(getListView());
    }

    private void populateData() {
    	GlobalState gs = (GlobalState) getApplication();
    	Cursor cursor = mDbHelper.fetchAllItems();
    	startManagingCursor(cursor);
    	
    	String[] from = new String[] {ShoppingListDbAdapter.KEY_ITEM};
    	int[] to = new int[]{R.id.text1};
    	
    	//setting up and array adapter and set it to display
    	SimpleCursorAdapter items = new SimpleCursorAdapter(this, R.layout.items_row, cursor, from, to);
    	setListAdapter(items);	
    	gs.setItems(getAllItems());
	}
    
    /**
     * Define the Context menu that can handle each item in the context and add the menu to the 
     * registered context view.
     * @param menu context menu only thing that we care in this method.
     */
    @Override
    public void onCreateContextMenu(ContextMenu menu, View view, ContextMenu.ContextMenuInfo menuInfo) {
    	super.onCreateContextMenu(menu, view, menuInfo);
    	menu.add(0, DELETE_ID, 0, R.string.remove);
    }
    
    /**
     * Handle the call back function when the context menu get selected for each item, make 
     * database call to delete the item from the database and populate the list.
     */
    @Override
    public boolean onContextItemSelected(MenuItem menuItem) {
    	boolean result = super.onContextItemSelected(menuItem);
    	switch(menuItem.getItemId()) {
    	case DELETE_ID:
    		AdapterContextMenuInfo adptInfo = (AdapterContextMenuInfo) menuItem.getMenuInfo();
    		mDbHelper.deleteItem(adptInfo.id);
    		populateData();
    		return true;
    	}
    	return result;
    }

    /**
     * Menu with the add functionality and add the item to the menu.
     * @param menu menu inherited from the super class.
     * @return result 
     */
	@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        boolean result = super.onCreateOptionsMenu(menu);
        menu.add(0, INSERT_ID, 0, R.string.add_new);
        return result;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    	switch (item.getItemId()) {
		case INSERT_ID:
			createItem();
			return true;
		}
        return super.onOptionsItemSelected(item);
    }

    /**
     * Create item and add to the database.
     */
	private void createItem() {
		String item = myEditText.getText().toString();
		mDbHelper.createItem(item);
		myEditText.setText("");
		populateData();
	}
	
	public ArrayList<String> getAllItems() {
		
		ArrayList<String> items = new ArrayList<String>();
    	//get all the items from the database and generate list
    	Cursor cursor = mDbHelper.fetchAllItems();
    	startManagingCursor(cursor);
    	cursor.moveToFirst();
    	while (cursor.isAfterLast() == false) {
    		items.add(cursor.getString(1));
    		Log.v("item:", cursor.getString(1));
            cursor.moveToNext();
    	}
    	return items;
	}
}
